import { updateUserInfo } from "../../../services/user";

Page({
  data: {
    nickname: '',
    btnLoading: false,
    btnDisabled: false,
  },
  onLoad(options: any) {
    const { name } = options;
    if (name) {
      this.setData({ nickname: name });
    } else {
      this.btnClose();
    }
  },

  onSubmit() {
    updateUserInfo({ nickname: this.data.nickname }).then(() => {
      this.goBack()
    });
  },

  onInputClear() {
    this.setData({ nickname: '' });
    this.btnClose();
  },

  onInputChange(e: any) {
    if (e.detail.value) {
      this.setData({ nickname: e.detail.value })
      this.btnOpen();
    } else {
      this.btnClose();
    }
  },

  // 返回上一页
  goBack() {
    const pages = getCurrentPages()
    if (pages.length > 1) {
      wx.navigateBack({ delta: 1 })
    } else {
      wx.reLaunch({ url: '/pages/ucenter/index' })
    }
  },

  btnClose() {
    this.setData({ btnDisabled: true });
  },

  btnOpen() {
    this.setData({ btnDisabled: false });
  }
});
